package com.itcast.springboot.service.impl;

import com.itcast.springboot.bean.Friend;
import com.itcast.springboot.bean.User;
import com.itcast.springboot.mapper.FriendMapper;
import com.itcast.springboot.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class FriendServiceImpl implements FriendService {
    @Autowired
    FriendMapper friendMapper;

    @Override
    public Integer insertFriend(Friend friend) {
        return friendMapper.insertFriend(friend);
    }

    @Override
    public Integer deleteFriend(Integer fid) {
        return friendMapper.deleteFriend(fid);
    }

    @Override
    public Integer deleteFriendByUidAndFid(String uid, String friendId) {
        return friendMapper.deleteFriendByUidAndFid(uid, friendId);
    }

    @Override
    public Friend getFriendById(Integer fid) {
        return friendMapper.getFriendById(fid);
    }

    @Override
    public List<Friend> getFriendsAndNames(String uid) {
        return friendMapper.getFriendsAndNames(uid);
    }

    @Override
    public List<String> isFriendsByUids(String uid, List<String> fids) {
        List<String> isFriends = friendMapper.isFriendsByUids(uid, fids);
        List<String> existFids = friendMapper.existFidsByUids(uid, fids);
        List<String> friends = new ArrayList<>();
        int i = 0;
        for (i = 0; i < existFids.size() - 1; i++) {
            //有两个相同的就存为true
            if (existFids.get(i).equals(existFids.get(i + 1))) {
                friends.add("true");
                i++;
                continue;
            }
            //保存原来的值
            friends.add(isFriends.get(i));
        }
        if (i == existFids.size() - 1) {
            //保存最后一个
            friends.add(isFriends.get(isFriends.size() - 1));
        }
        return friends;
    }

    @Override
    public List<Integer> getCountsByfids(List<String> fids) {
        return friendMapper.getCountsByfids(fids);
    }

    @Override
    public Integer getExistFriend(String uid, String fid) {
        return friendMapper.getExistFriend(uid, fid);
    }

    @Override
    public Integer updateFriend(Friend friend) {
        return friendMapper.updateFriend(friend);
    }
}
